﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using Caliburn.Micro;
using AvalonDock;
using Agnes.Core;
using Agnes.Libaray;

namespace Agnes.TaskManager
{
    /// <summary>
    /// 新建任务命令
    /// </summary>
    public class NewTaskCmd : PropertyChangedBase
    {
        private static NewTaskCmd _instance;

        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static NewTaskCmd Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new NewTaskCmd();
                return _instance;
            }
        }

        private RoutedCommand _requery;

        /// <summary>
        /// 是否能执行
        /// </summary>
        public bool CanExcuted { get; set; }

        NewTaskCmd()
        {
            CanExcuted = true;
            _requery = new RoutedCommand("New Normal Task", typeof(NewTaskCmd));
        }

        /// <summary>
        /// 新建任务的路由命令
        /// </summary>
        public static RoutedCommand Requery
        {
            get
            {
                return Instance._requery;
            }
        }

        /// <summary>
        /// 新建任务执行代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            TaskInfoEditViewModel vm = new TaskInfoEditViewModel();
            TaskInfoEditView v = new TaskInfoEditView();
            ViewModelBinder.Bind(vm, v, null);
            DocumentContent dc = new DocumentContent();
            dc.Content = v;
            dc.Title = "新建任务";
            dc.Tag = vm.GetHashCode();
            dc.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(vm.OnClosing);
            dc.Show(AgnesWorkBenchService.DockingManager, false);
        }


        /// <summary>
        /// 判断是否能执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (SlnService.Instance.CurrentSln == null)
                e.CanExecute = false;
            else
                e.CanExecute = CanExcuted;
        }
    }
}
